package main;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.ds.DSFactory;
import cn.hutool.db.ds.druid.DruidDSFactory;
import lombok.SneakyThrows;
import utils.SmallTool;

import javax.sql.DataSource;
import java.util.List;

/**
 * @Author: gcr
 * @Date: 2022/3/12 14:16
 */
public class UpdateNull {

    public static String table = "pmid_doi_new_new";

    @SneakyThrows
    public static void main(String[] args) {

        // hutool 测试赋值null

        int slze = 73037;


        //  2021年11月18日15:41:15 插入2800万以后的数据,进行查询 没有的才插入,有就先不更新了
        //先删除 后插入

        DSFactory.setCurrentDSFactory(new DruidDSFactory());
        DataSource ds;
        int startPmid = 0;
        Db use = Db.use();
        String allDate = "select max(pmid) as pmid  from " + table;
        Entity query = use.query(allDate).get(0);
        SmallTool.printMess("最大的id 是" + query.getStr("pmid"));

        Integer count = query.getInt("pmid");
        int breakSize = 100000;
        int pageSize = count / breakSize + 1;
        SmallTool.printMess("全部页码+++++++++" + pageSize);
        SmallTool.printMess("即将开始请提前关闭如果想的话");
        Thread.sleep(10000);

        SmallTool.printMess("任务开始+++++++++++++++++++++++++++++++++++++++++++++++++++++");
        for (int i = 1; i <= pageSize; i++) {
            SmallTool.printMess("当前页码+++++" + i);
            int start = breakSize * (i - 1) + 1;
            int end = i * breakSize;
            SmallTool.printMess("开始id++" + start + "+++结束id++++" + end);
            if (end <= startPmid) {
                SmallTool.printMess("因为小于" + startPmid + " 略过了");
                continue;
            }
            String paramSql = "select pmid,doi from " + table + " where  pmid between  " + start + " and " + end;
            List<Entity> querys = use.query(paramSql);
            querys.parallelStream().forEach(entity -> {
                Integer pmid = entity.getInt("pmid");
                try {
                    updateData(pmid, 22222 + "", use);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            });
        }

    }

    @SneakyThrows
    public static void updateData(Integer pmid, String doi, Db use) {
        Entity whereEn = new Entity();
        whereEn.setTableName(table);
        whereEn.set("pmid", pmid);
        int doi1 = use.update(new Entity()
                .set("doi", doi), whereEn
        );
//        SmallTool.printMess("更新了数量++pmid++"+pmid+"++"+doi1);
    }


}
